package l.l.w.CodingInterviews;

/**
 * Created by llw on 2018/4/9/0009.
 */
public class 翻转单词顺序列 {
    public String ReverseSentence(String str) {
        if(str == null || str.length() == 0 || "".equals(str) || " ".equals(str)) return str;
        String[] strs = str.split(" ");
        StringBuilder sb = new StringBuilder(str.length() + 1);
        for(int i = strs.length - 1; i >= 0; --i){
            sb.append(strs[i]);
            sb.append(" ");
        }
        if(sb.length() > 1){
            sb.delete(sb.length() - 1, sb.length());
        }
        if(sb.length() == 0){
            sb.append(" ");
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String str = " ";
        String s = new 翻转单词顺序列().ReverseSentence(str);
        System.out.println(s);
    }
}
